package com.flying.cms.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.flying.common.annotation.CommonQuery;
import com.flying.common.annotation.CommonUpdate;
import com.flying.common.annotation.CommonUpdate.Position;
import com.flying.common.util.Codes;
import com.flying.common.util.StringUtils;
import com.flying.common.util.Utils;
import com.flying.framework.annotation.Param;
import com.flying.framework.data.Data;
import com.flying.framework.service.AbstractService;

public class CategoryService  extends AbstractService{

	//@RemoveCache(tag="cms.Category")
	@CommonUpdate(position=Position.after_body, 
		params={"$SEQ.CMS.CATEGORY", "parent_id:0", "code", "name", "icon", "xfields", "keywords", "desc","sort_id:0", "creator"})
	public Data create(Data request) throws Exception {
		request.remove(Codes.CODE);
		request.put("parent_id", request.getLong("parent_id", 0L));
		return request;
	}

	//@RemoveCache(tag="cms.Category")
	@CommonUpdate(position=Position.after_body, 
		params={"parent_id:0", "code", "name", "icon", "xfields", "keywords", "desc","sort_id:0", "updator", "category_id:0"})
	public Data update(Data request) throws Exception {
		final String icon = request.getString("icon");
		if(StringUtils.isEmpty(icon)) {
			Data cat = this.findById(request.getString("category_id"));
			if(cat != null)
				request.put("icon", cat.getString("icon"));
		}
		request.remove(Codes.CODE);
		return request;
	}

	//@RemoveCache(tag="cms.Category")
	@CommonUpdate(params={"category_id:0"})
	public Data remove(@Param("category_id")String categoryId) throws Exception {
		return null;
	}

	//@UseCache(tag="cms.Category")
	@CommonQuery(params={})
	public Data findAll(Data request) throws Exception {
		return request;
	}

	//@UseCache(tag="cms.Category",keys={"code"})
	@CommonQuery(single=true,
				params={"code"},
				qsql="select * from t_category where code=?"
			)
	public Data findByCode(Data request) throws Exception {
		request.put("rows", request.getInteger("rows", Integer.MAX_VALUE));
		return request;
	}

	//@UseCache(tag="cms.Category",keys={"category_id"})
	@CommonQuery(single=true,
				params={"category_id"},
				qsql="select * from t_category where category_id=?"
				)
	public Data findById(@Param("category_id")String category_id) throws Exception {
		return null;
	}

	//@UseCache(tag="cms.Category",keys={"parent_id"})
	@CommonQuery(
					position=CommonQuery.Position.after_body, 
					params={"parent_id:0"},
					qsql="select * from t_category where parent_id=? order by sort_id desc limit ? , ?",
					csql="select count(1) from t_category where parent_id=?"
					)
	public Data findChildren(Data request) throws Exception {
		request.put("rows", request.getInteger("rows", Integer.MAX_VALUE));
		return request;
	}
	
	//@UseCache(tag="cms.Category",keys={"parent_id"})
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Data findChildrenTree(Data request) throws Exception {
		final String cid = request.getString("category_id");
		request.put("rows", Integer.MAX_VALUE);
		final List<Map> rows = findChildren(request).get("rows");
		for(Iterator<Map> it = rows.iterator(); it.hasNext(); ) {
			Map map = it.next();
			int category_id = (Integer)map.get("category_id");
			String text = (String)map.get("name");
			map.put("id", String.valueOf(category_id));
			map.put("text", text);
			if(StringUtils.equals(cid, String.valueOf(category_id))) {
				map.put("selected", true);
			}
			List<Data> children = findChildrenTree(new Data("parent_id", category_id, "category_id", cid, "rows", Integer.MAX_VALUE)).get("rows");
			if(children != null && !children.isEmpty())
				map.put("children", children);
		}
		return new Data(Codes.CODE, Codes.SUCCESS, "rows", rows, "$value", "rows");
	}
	//
	@CommonQuery(
					position=CommonQuery.Position.after_body, 
					params={"code"},
					qsql="select * from t_category where parent_id in(select category_id from t_category where code=?) order by sort_id desc limit ? , ?",
					csql="select count(1) from t_category where parent_id in(select category_id from t_category where code=?)"
					)
	public Data findChildrenByCode(Data request) throws Exception {
		request.put("rows", request.getInteger("rows", Integer.MAX_VALUE));
		return request;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Data findChildrenTreeByCode(Data request) throws Exception {
		request.put("rows", Integer.MAX_VALUE);
		final List<Map> rows = findChildrenByCode(request).get("rows");
		for(Iterator<Map> it = rows.iterator(); it.hasNext(); ) {
			Map map = it.next();
			int category_id = (Integer)map.get("category_id");
			String code = (String)map.get("code");
			String text = (String)map.get("name");
			map.put("id", category_id);
			map.put("text", text);
			map.put("children", findChildrenTreeByCode(new Data("code", code, "rows", Integer.MAX_VALUE)).get("rows"));
		}
		return new Data(Codes.CODE, Codes.SUCCESS, "rows", rows, "$value", "rows");
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Data findChildrenTreeByCodeWithSelf(Data request) throws Exception {
		Data self = this.findByCode(request);
		
		final List<Map> rows = Utils.newArrayList();
		final Map map = Utils.newHashMap();
		
		int category_id = (Integer)self.get("category_id");
		String code = (String)self.get("code");
		String text = (String)self.get("name");
		map.put("id", category_id);
		map.put("text", text);
		map.put("children", findChildrenTreeByCode(new Data("code", code, "rows", Integer.MAX_VALUE)).get("rows"));
		
		rows.add(map);
		
		return new Data(Codes.CODE, Codes.SUCCESS, "rows", rows, "$value", "rows");
	}
}
